﻿// ReSharper disable MemberCanBeMadeStatic.Global
namespace KinonekoSoftware.Studio.Models.Modules
{
    public abstract class ModulePresenter : ObservableObject
    {
        private static bool _isDisplayMode;
        private        int  _index;

        protected ModulePresenter()
        {
            _isDisplayMode   = true;
            EditCommand      = new ActionCommand(DoEditCommand);
            ShiftUpCommand   = new ActionCommand(DoShiftUpCommand);
            ShiftDownCommand = new ActionCommand(DoShiftDownCommand);
            RemoveCommand    = new ActionCommand(DoRemoveCommand);
        }

        private async void DoEditCommand()
        {
            var r = await OnEditItem();

            if (r)
            {
                Queue?.OnNext(new SaveModuleMessage());
            }
        }

        private void DoShiftUpCommand()
        {
            Queue?.OnNext(new PositionChangedMessage
            {
                IsUpDirection = true,
                Block         = this,
            });
        }

        private void DoShiftDownCommand()
        {
            Queue?.OnNext(new PositionChangedMessage
            {
                IsUpDirection = false,
                Block         = this,
            });
        }

        private async void DoRemoveCommand()
        {
            if (!await RemoveItem())
            {
                return;
            }

            if (Self is RVEGroupPresenter)
            {
                return;
            }

            Queue?.OnNext(new RemoveModuleMessage
            {
                Block = this,
            });
        }

        /// <summary>
        /// 指向自身的属性
        /// </summary>
        private ModulePresenter Self => this;

        protected Task<bool> RemoveItem(string title = null, string description = null)
        {

            if (string.IsNullOrEmpty(title))
            {
                title = LanguageManager.GetText("Title.Deleting");
            }
            if (string.IsNullOrEmpty(description))
            {
                description = LanguageManager.GetText("Text.Remove.Item");
            }

            return ViewModelBase.DangerOperation(title, description);
        }

        /// <summary>
        /// 快速返回当前元素的标题或名字
        /// </summary>
        /// <returns>返回当前内容块的标题。</returns>
        public abstract string GetTitle();
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public abstract bool IsMultipleVariants();
        
        /// <summary>
        /// 获得当前内容块的所有变量。
        /// </summary>
        /// <param name="documentID">指定的设定ID</param>
        /// <returns>返回当前内容块的所有变量。</returns>
        public abstract IEnumerable<Variant> GetVariants(string documentID);
        
        /// <summary>
        /// 获得当前内容块的变量。
        /// </summary>
        /// <param name="documentID">指定的设定ID</param>
        /// <returns>返回当前内容块的变量。</returns>
        public abstract Variant GetVariant(string documentID);

        /// <summary>
        /// 接受外部的喵喵咒语更改
        /// </summary>
        /// <param name="variant"></param>
        public abstract void DataSync(Variant variant);

        /// <summary>
        /// 当索引值改变时。
        /// </summary>
        /// <param name="oldIndex"></param>
        /// <param name="newIndex"></param>
        protected virtual void OnIndexChanged(int oldIndex, int newIndex)
        {

        }

        protected virtual Task<bool> OnEditItem()
        {
            return Task.FromResult(false);
        }


        /// <summary>
        /// 设置编辑模式。
        /// </summary>
        /// <param name="value">是否为编辑模式。</param>
        public void SetIsDisplayMode(bool value) => _isDisplayMode = value;
        
        
        /// <summary>
        /// 更新编辑模式的状态。
        /// </summary>
        public void UpdateIsDisplayMode() => RaiseUpdated(nameof(IsDisplayMode));

        /// <summary>
        /// 获取或设置 <see cref="Index"/> 属性。
        /// </summary>
        public int Index
        {
            get => _index;
            set
            {
                OnIndexChanged(_index, value);
                SetValue(ref _index, value);
            }
        }
        /// <summary>
        /// 是否为截图模式
        /// </summary>
        public bool IsDisplayMode => _isDisplayMode;

        public IModuleMessageBus Queue            { get; set; }
        public ICommandEX        EditCommand      { get; }
        public ICommandEX        ShiftUpCommand   { get; }
        public ICommandEX        ShiftDownCommand { get; }
        public ICommandEX        RemoveCommand    { get; }
    }
}